#include "DCLinkList.h"
#include <stdio.h>
#include <stdlib.h>
#define true 1
#define false 0

int InitDCLinkList(DCLinkList *list)
{
    list->len=0;
    list->head=NULL;
    list->tail=NULL;
    return true;
}

Node *CreateNode(ElementType element)
{
    Node *newnode=(Node*)malloc(sizeof(Node));
    if(newnode==NULL)
    {
        printf("creatnode malloc error\n");
        return NULL;
    }
    newnode->data=element;
    newnode->next=NULL;
    newnode->prev=NULL;
    return newnode;
}


void InsertTail(DCLinkList *list, ElementType element)
{
    Node *newnode=CreateNode(element);
    if(newnode==NULL)
    {
        printf("insertail createnode error\n");
        return;
    }
    if(list->len==0)
    {
        list->head=newnode;
        list->tail=newnode;
    }
    else
    {
        list->tail->next=newnode;
        newnode->prev=list->tail;
        list->tail=newnode;
        //首尾相连
        newnode->next=list->head;
        list->head->prev=newnode;
    }
        list->len++;
}

void InsertHead(DCLinkList *list, ElementType element)
{
    InsertTail(list,element);
    if(list->len!=1)
    {
        list->head=list->tail;
        list->tail=list->tail->prev;
    }
}

int FindFirstByElement(DCLinkList *list, ElementType element)
{
    int count=0;
    Node *TravelPoint=list->head;
    while(TravelPoint!=list->tail)
    {
        if(TravelPoint->data==element)
        {
            return count;
        }
        count++;
        TravelPoint=TravelPoint->next;
    }
    if(list->tail!=NULL&&list->tail->data==element)
    {
        return count;
    }
    return -1;    
}

void RemoveByIndex(DCLinkList *list, int index)
{
    if(index<0||index>=list->len)
    {
        printf("removebyindex invalid error\n");
        return;
    }
    if(index==0)
    {
        Node *temp=list->head;
        if(list->len==1)
        {
            free(temp);
            list->head=NULL;
            list->tail=NULL;
            list->len=0;
            return;
        }
        list->head=list->head->next;
        list->head->prev=list->tail;
        list->tail->next=list->head;
        free(temp);
        list->len--;
        return;
    }
    if(index==list->len-1)
    {
        Node *temp=list->tail;
        list->tail=list->tail->prev;
        list->tail->next=list->head;
        list->head->prev=list->tail;
        list->len--;
        free(temp);
        return;
    }
    Node *TravelPoint=list->head;
    while(index!=0)
    {
        TravelPoint=TravelPoint->next;
        index--;
    }
    
    TravelPoint->prev->next=TravelPoint->next;
    TravelPoint->next->prev=TravelPoint->prev;
    free(TravelPoint);
    list->len--;
}

void RemoveByElement(DCLinkList *list, ElementType element)
{
    int count=FindFirstByElement(list,element);
    while(count!=-1)
    {
        RemoveByIndex(list,count);
        count=FindFirstByElement(list,element);
    }
}

void Travel(DCLinkList *list)
{
    if(list->len==0)
    {
        printf("the linklist is empty\n");
        return;
    }
    printf("next:");
    Node *TravelPoint=list->head;
    while(TravelPoint!=list->tail)
    {
        printf("%d ",TravelPoint->data);
        TravelPoint=TravelPoint->next;
    }
    printf("%d ",list->tail->data);
    printf("\n");
    printf("prev:");
    TravelPoint=list->tail;
    while(TravelPoint!=list->head)
    {
        printf("%d ",TravelPoint->data);
        TravelPoint=TravelPoint->prev;
    }
    printf("%d ",list->head->data);
    printf("\n");

}

void FreeDCLinkList(DCLinkList *list)
{
    while(list->head!=list->tail)
    {
        Node *temp=list->head->next;
        free(list->head);
        list->head=temp;
    }
    free(list->tail);
    list->len=0;
    list->head=NULL;
    list->tail=NULL;
}
